package com.healthcloud.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.healthcloud.dao.HcDietaryNutritionMapper;
import com.healthcloud.dao.HcHealthStatusAndOccupationalHistoryMapper;
import com.healthcloud.dao.HcLifeStyleAndHabitsMapper;
import com.healthcloud.dao.HcSportModeMapper;
import com.healthcloud.entity.HcDietaryNutrition;
import com.healthcloud.entity.HcHealthStatusAndOccupationalHistory;
import com.healthcloud.entity.HcLifeStyleAndHabits;
import com.healthcloud.entity.HcSportMode;
import com.healthcloud.service.HealthSurveyService;

/**
 * 健康调查接口实现类
 * @author ZhengL 
 * 2015-07-10
 */
@Service
@Transactional
public class HealthSurveyServiceImpl implements HealthSurveyService {

	@Autowired
	private HcDietaryNutritionMapper hcDietaryNutritionMapper;
	@Autowired
	private HcHealthStatusAndOccupationalHistoryMapper hcHealthStatusAndOccupationalHistoryMapper;
	@Autowired
	private HcLifeStyleAndHabitsMapper hcLifeStyleAndHabitsMapper;
	// private HcPhysicalExamAndLaboratoryExamMapper
	// hcPhysicalExamAndLaboratoryExamMapper;
	@Autowired
	private HcSportModeMapper hcSportModeMapper;

	@Override
	public Object parseJsonMap(Map<String, String> jsonMap) {
		String op = jsonMap.get("op");
		if (op == "hc_dietarynutrition" || op.equals("hc_dietarynutrition")) {
			return getObject1(jsonMap);
		} else if (op == "hc_healthstatusandoccupationalhistory"
				|| op.equals("hc_healthstatusandoccupationalhistory")) {
			return getObject2(jsonMap);
		} else if (op == "hc_lifestyleandhabits"
				|| op.equals("hc_lifestyleandhabits")) {
			return getObject3(jsonMap);
		} else {
			return getObject4(jsonMap);
		}
	}

	//膳食营养
	private HcDietaryNutrition getObject1(Map<String, String> jsonMap) {
		HcDietaryNutrition obj = new HcDietaryNutrition();
		if (jsonMap != null && !jsonMap.isEmpty()) {
			String actId = jsonMap.get("id");
			if (!StringUtil.isBlank(actId)) {
				Long id = Long.valueOf(actId);
				if (id > 0) {
					obj = hcDietaryNutritionMapper.get(id);
				}
			}
			if (!StringUtil.isBlank(jsonMap.get("examId"))){
				obj.setMkExamCode(jsonMap.get("examId"));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer1"))){
				obj.setAnswer1(Integer.valueOf(jsonMap.get("answer1")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer2"))){
				obj.setAnswer2(Integer.valueOf(jsonMap.get("answer2")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer3"))){
				obj.setAnswer3(Integer.valueOf(jsonMap.get("answer3")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer4"))){
				obj.setAnswer4(Integer.valueOf(jsonMap.get("answer4")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer5"))){
				obj.setAnswer5(Integer.valueOf(jsonMap.get("answer5")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer6"))){
				obj.setAnswer6(Integer.valueOf(jsonMap.get("answer6")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer7"))){
				obj.setAnswer7(Integer.valueOf(jsonMap.get("answer7")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer8"))){
				obj.setAnswer8(Integer.valueOf(jsonMap.get("answer8")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer9"))){
				obj.setAnswer9(Integer.valueOf(jsonMap.get("answer9")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer10"))){
				obj.setAnswer10(Integer.valueOf(jsonMap.get("answer10")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer11"))){
				obj.setAnswer11(Integer.valueOf(jsonMap.get("answer11")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer12"))){
				obj.setAnswer12(Integer.valueOf(jsonMap.get("answer12")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer13"))){
				obj.setAnswer13(Integer.valueOf(jsonMap.get("answer13")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer14"))){
				obj.setAnswer14(Integer.valueOf(jsonMap.get("answer14")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer15"))){
				obj.setAnswer15(Integer.valueOf(jsonMap.get("answer15")));
			}
			if (StringUtil.isBlank(actId) || actId == "0" || actId.equals("0")) {
				hcDietaryNutritionMapper.save(obj);
			} else {
				hcDietaryNutritionMapper.update(obj);
			}
		}
		return obj;
	}

	//健康状况及职业史
	private HcHealthStatusAndOccupationalHistory getObject2(
			Map<String, String> jsonMap) {
		HcHealthStatusAndOccupationalHistory obj = new HcHealthStatusAndOccupationalHistory();
		if (jsonMap != null && !jsonMap.isEmpty()) {
			String actId = jsonMap.get("id");
			if (!StringUtil.isBlank(actId)) {
				Long id = Long.valueOf(actId);
				if (id > 0) {
					obj = hcHealthStatusAndOccupationalHistoryMapper.get(id);
				}
			}
			if (!StringUtil.isBlank(jsonMap.get("examId"))){
				obj.setMkExamCode(jsonMap.get("examId"));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer1"))){
				obj.setAnswer1(Integer.valueOf(jsonMap.get("answer1")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer2"))){
				obj.setAnswer2(Integer.valueOf(jsonMap.get("answer2")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer3"))){
				obj.setAnswer3(Integer.valueOf(jsonMap.get("answer3")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer4"))){
				obj.setAnswer4(Integer.valueOf(jsonMap.get("answer4")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer5"))){
				obj.setAnswer5(Integer.valueOf(jsonMap.get("answer5")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer6"))){
				obj.setAnswer6(Integer.valueOf(jsonMap.get("answer6")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer7"))){
				obj.setAnswer7(Integer.valueOf(jsonMap.get("answer7")));
			}
			if (StringUtil.isBlank(actId) || actId == "0" || actId.equals("0")) {
				hcHealthStatusAndOccupationalHistoryMapper.save(obj);
			} else {
				hcHealthStatusAndOccupationalHistoryMapper.update(obj);
			}
		}
		return obj;
	}

	//生活方式及习惯
	private HcLifeStyleAndHabits getObject3(Map<String, String> jsonMap) {
		HcLifeStyleAndHabits obj = new HcLifeStyleAndHabits();
		if (jsonMap != null && !jsonMap.isEmpty()) {
			String actId = jsonMap.get("id");
			if (!StringUtil.isBlank(actId)) {
				Long id = Long.valueOf(actId);
				if (id > 0) {
					obj = hcLifeStyleAndHabitsMapper.get(id);
				}
			}
			if (!StringUtil.isBlank(jsonMap.get("examId"))){
				obj.setMkExamCode(jsonMap.get("examId"));
			}
			if (!StringUtil.isBlank(jsonMap.get("answerA1"))){
				obj.setAnswerA1(Integer.valueOf(jsonMap.get("answerA1")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answerA2"))){
				obj.setAnswerA2(Integer.valueOf(jsonMap.get("answerA2")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answerA3"))){
				obj.setAnswerA3(Integer.valueOf(jsonMap.get("answerA3")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answerA4"))){
				obj.setAnswerA4(Integer.valueOf(jsonMap.get("answerA4")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answerB1"))){
				obj.setAnswerB1(Integer.valueOf(jsonMap.get("answerB1")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answerB2"))){
				obj.setAnswerB2(Integer.valueOf(jsonMap.get("answerB2")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answerC1"))){
				obj.setAnswerC1(Integer.valueOf(jsonMap.get("answerC1")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answerC2"))){
				obj.setAnswerC2(Integer.valueOf(jsonMap.get("answerC2")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answerC3"))){
				obj.setAnswerC3(Integer.valueOf(jsonMap.get("answerC3")));
			}
			if (StringUtil.isBlank(actId) || actId == "0" || actId.equals("0")) {
				hcLifeStyleAndHabitsMapper.save(obj);
			} else {
				hcLifeStyleAndHabitsMapper.update(obj);
			}
		}
		return obj;
	}

	//运动方式
	private HcSportMode getObject4(Map<String, String> jsonMap) {
		HcSportMode obj = new HcSportMode();
		if (jsonMap != null && !jsonMap.isEmpty()) {
			String actId = jsonMap.get("id");
			if (!StringUtil.isBlank(actId)) {
				Long id = Long.valueOf(actId);
				if (id > 0) {
					obj = hcSportModeMapper.get(id);
				}
			}
			if (!StringUtil.isBlank(jsonMap.get("examId"))){
				obj.setMkExamCode(jsonMap.get("examId"));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer1"))){
				obj.setAnswer1(Integer.valueOf(jsonMap.get("answer1")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer2"))){
				obj.setAnswer2(Integer.valueOf(jsonMap.get("answer2")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer3"))){
				obj.setAnswer3(Integer.valueOf(jsonMap.get("answer3")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer4"))){
				obj.setAnswer4(Integer.valueOf(jsonMap.get("answer4")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer5"))){
				obj.setAnswer5(Integer.valueOf(jsonMap.get("answer5")));
			}
			if (!StringUtil.isBlank(jsonMap.get("answer6"))){
				obj.setAnswer6(Integer.valueOf(jsonMap.get("answer6")));
			}
			if (StringUtil.isBlank(actId) || actId == "0" || actId.equals("0")) {
				hcSportModeMapper.save(obj);
			} else {
				hcSportModeMapper.update(obj);
			}
		}
		return obj;
	}

	@Override
	public HcDietaryNutrition findEntity1(String mkCode){
		HcDietaryNutrition entity = new HcDietaryNutrition();
		HcDietaryNutrition temp = new HcDietaryNutrition();
		if(StringUtils.isNotBlank(mkCode)){
			temp.setMkExamCode(mkCode);
		}
		List<HcDietaryNutrition> list = hcDietaryNutritionMapper.findByEntity(temp, null);
		if(list!=null&&list.size()>0){
			entity = list.get(0);
		}
		return entity;
	}
	
	@Override
	public  HcHealthStatusAndOccupationalHistory findEntity2(String mkCode){
		HcHealthStatusAndOccupationalHistory entity = new HcHealthStatusAndOccupationalHistory();
		HcHealthStatusAndOccupationalHistory temp = new HcHealthStatusAndOccupationalHistory();
		if(StringUtils.isNotBlank(mkCode)){
			temp.setMkExamCode(mkCode);
		}
		List<HcHealthStatusAndOccupationalHistory> list = hcHealthStatusAndOccupationalHistoryMapper.findByEntity(temp, null);
		if(list!=null&&list.size()>0){
			entity = list.get(0);
		}
		return entity;
	}
	
	@Override
	public  HcLifeStyleAndHabits findEntity3(String mkCode){
		HcLifeStyleAndHabits entity = new HcLifeStyleAndHabits();
		HcLifeStyleAndHabits temp = new HcLifeStyleAndHabits();
		if(StringUtils.isNotBlank(mkCode)){
			temp.setMkExamCode(mkCode);
		}
		List<HcLifeStyleAndHabits> list = hcLifeStyleAndHabitsMapper.findByEntity(temp, null);
		if(list!=null&&list.size()>0){
			entity = list.get(0);
		}
		return entity;
	}
	
	@Override
	public HcSportMode findEntity4(String mkCode){
		HcSportMode entity = new HcSportMode();
		HcSportMode temp = new HcSportMode();
		if(StringUtils.isNotBlank(mkCode)){
			temp.setMkExamCode(mkCode);
		}
		List<HcSportMode> list = hcSportModeMapper.findByEntity(temp, null);
		if(list!=null&&list.size()>0){
			entity = list.get(0);
		}
		return entity;
	}
}
